//
//  TestOrm.m
//  JhonSell
//
//  Created by Mario Montoya on 8/01/09.
//  Copyright 2009 El malabarista. All rights reserved.
//

#import "DbObject.h"
#import	"SamplePerson.h"
#import "NSString+Clean.h"
#import "TestBaseORM.h"

@interface TestOrm : TestBaseORM { }

-(void) testIntrospection;
-(void) testFill;
-(void) testLoadFill;
-(void) testLoadByFieldAndValue;
-(void) testLoadById;
-(void) testDelete;
-(void) testSave;

@end

@implementation TestOrm

-(void) setUp {
	[super setUp];

	Db *db = [Db currentDb];
	
	[db execute:@"CREATE TABLE IF NOT EXISTS SamplePerson"
	 @"(id INTEGER PRIMARY KEY  AUTOINCREMENT  NOT NULL,name CHAR,"
	 @"birthDate Date,size INTEGER,"
	 @"phone INTEGER,balance DOUBLE, isOk BOOLEAN, IdSampleCity INTEGER, timeStamp Date)"];
	
	[db execute:@"DELETE FROM SamplePerson"];
}

-(void) testIntrospection {
	SamplePerson *o = [[SamplePerson alloc] init];
	
	NSArray *fields = [[[SamplePerson propertiesAsStringList] componentsSeparatedByString:@", "] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
	NSArray *compare = [[NSArray arrayWithObjects:@"birthDate", @"balance", @"Id", @"isOk", 
								@"name", @"phone", @"city", @"timeStamp", @"size",nil] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
	
	GHAssertEqualStrings(o.tableName,@"SamplePerson",@"Incorrect name");
	GHAssertEqualStrings([NSString arrayToString:fields],[NSString arrayToString:compare],@"Fail to load the properties names");

	[o release];
}

-(void) testDelete {
	Db *db = [Db currentDb];

	SamplePerson *o1 = [[SamplePerson alloc] init];
	SamplePerson *o2 = [[SamplePerson alloc] init];
	
	o1.name = @"Sample1";
	o2.name = @"Sample2";
		
	GHAssertEquals([db save:o1],YES,@"Record not saved");
	GHAssertEquals([db save:o2],YES,@"Record not saved");
	
	GHAssertEquals([db count:[o1 tableName]],2,@"Row not saved");
	
	[db forceDel:o1];
	
	GHAssertEquals([db count:[o1 tableName]],1,@"Row not deleted");

	[o1 release];	
	[o2 release];
}

-(void) testLoadFill {
	Db *db = [Db currentDb];
	
	SamplePerson *o1 = [[SamplePerson alloc] init];
	SamplePerson *o2 = [[SamplePerson alloc] init];
	
	o1.name = @"Sample1";
	o2.name = @"Sample2";

	[db save:o1];
	[db save:o2];
		
	NSArray * persons = [db loadAndFill:@"SELECT * FROM SamplePerson" theClass:[SamplePerson class]];
	
	GHAssertTrue([persons count]==2,@"Must load 2 records");

	GHAssertEqualStrings([[persons objectAtIndex:0] name],@"Sample1",@"Wrong row");
	GHAssertEqualStrings([[persons objectAtIndex:1] name],@"Sample2",@"Wrong row");
	
	[o1 release];
	[o2 release];
}

-(void) testLoadByFieldAndValue {
	Db *db = [Db currentDb];

	[self testSave];
	
	SamplePerson *o = [db loadByFieldAndValue:[SamplePerson class] fieldName:@"name" value:@"Sample"];
	
	GHAssertNotNil(o,@"Record not found");
	GHAssertEqualStrings(o.name,@"Sample",@"Wrong record");
}

-(void) testLoadById {
	Db *db = [Db currentDb];
	
	[self testSave];
	NSInteger theId = [[db numericValue:@"SELECT Id FROM SamplePerson ORDER BY Id DESC"] intValue];
	SamplePerson *o = [db loadById: [SamplePerson class] theId:theId];
	
	GHAssertNotNil(o,@"Record not found");
	GHAssertEqualStrings(o.name,@"Sample",@"Wrong record");
}

-(void) testFill {
	Db *db = [Db currentDb];
	
	SamplePerson *o = [[SamplePerson alloc] init];
	
	[self testSave];
	
	FMResultSet *rs = [db load:@"select * from SamplePerson where name= 'Sample'"];
	
	GHAssertEquals([rs next],YES,@"Row not found");
	
	[db fill:o resultset:rs];
	NSDate *birthDate = [NSDate dateWithString:@"2009-12-10 00:00:00 +0000"];	
	
	GHAssertTrue(o.Id,@"Wrong value for Id");
	GHAssertEqualStrings(o.name, @"Sample", @"Wrong value for name");
	GHAssertTrue([o.birthDate isEqualToDate: birthDate],[NSString stringWithFormat:@"Wrong value for date: %@",birthDate]);
	GHAssertEquals(o.size,12,@"Wrong value for size");
	GHAssertEquals([o.phone intValue],5552222,@"Wrong value for phone");
	GHAssertEquals([o.balance doubleValue],12.5,@"Wrong value for balance");
	GHAssertEquals(o.isOk,YES,@"Wrong value for isOk");

	[o release];
}

-(void) testSave {
	Db *db = [Db currentDb];
	[db execute:@"DELETE FROM SamplePerson"];

	SamplePerson *o = [[SamplePerson alloc] init];
	
	o.name = @"Sample";
	o.birthDate = [NSDate dateWithString:@"2009-12-10 00:00:00 +0000"];
	o.size = 12;
	o.phone = [NSNumber numberWithInt: 5552222];
	o.balance = [NSDecimalNumber decimalNumberWithDecimal: [[NSNumber numberWithDouble: 12.5] decimalValue]];
	o.isOk = YES;
	//Only for get a repeteable test!!
	o.timeStamp = [NSDate dateWithString:@"2009-12-10 00:00:00 +0000"];
	
	GHAssertEquals([o isNew],YES,@"Is not marked as new");
	
	[db save:o];
	
	GHAssertEquals([db count:[o tableName]],1,@"Row not saved");
	
	GHAssertEquals([o isNew],NO,@"Is not marked as old");
	
	[o release];
}

@end
